package com.myweb.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.myweb.model.Menu;
import com.myweb.model.Teacher;
import com.myweb.model.User;
import com.myweb.service.UserService;


@RestController
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private UserService uService;
	
	@Value("${jdbc_url}")
	private String jdbc_url;
	
	/**
	 * 调用service
	 */
	@RequestMapping(value = "add")
	public void add(User entity){
		uService.add(entity);
	}
	
	/**
	 * 页面输出
	 */
	@RequestMapping(value = "save")
	public Map<String, Object> save(){
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	/**
	 * 实现跳转
	 * @return
	 */
	@RequestMapping(value = "login")
	public ModelAndView login(){
		return new ModelAndView("index.jsp");
	}
	
	/**
	 * 返回json对象
	 * @param request
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping(value = "find", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> find(HttpServletRequest request) throws Throwable {
		HashMap<String, Object> jsonmap=new HashMap<String, Object>();
		PageInfo<User> uPage=uService.find();
		jsonmap.put("success", true);
		jsonmap.put("uPage", uPage);
		return jsonmap;
	}
	
	@RequestMapping(value = "getjdbc_url")
	public String getjdbc_url(){
		return jdbc_url;
	}
	
	/**
	 * 一对多单个查询
	 * @param tId
	 * @return
	 */
	@RequestMapping(value = "selectByKey")
	public Teacher selectByKey(Integer tId){
		return uService.selectByKey(tId);
	}
	
	/**
	 * 一对多集合查询
	 * @return
	 */
	@RequestMapping(value = "select")
	public List<Teacher> select(){
		return uService.select();
	}
	
	/**
	 * 无限级菜单树查询
	 * @return
	 */
	@RequestMapping(value = "findMenuAll")
	public List<Menu> findMenuAll(){
		return uService.findMenuAll();
	}
}
